package org.jadestudio.dsdown.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.jadestudio.dsdown.entity.ManageAdminLogin;
import org.jadestudio.dsdown.entity.ManageGame;
import org.jadestudio.dsdown.entity.ManageGameDownloads;
import org.jadestudio.dsdown.entity.ManageGamePic;
import org.jadestudio.dsdown.entity.ManageGamePublishCompany;
import org.jadestudio.dsdown.entity.ManageGamePurchase;
import org.jadestudio.dsdown.entity.ManageHomePagePicCfg;
import org.jadestudio.dsdown.service.GameManageService;
import org.jadestudio.dsdown.service.ManageLoginService;
import org.jadestudio.dsdown.util.ISessionCheck;
import org.jadestudio.dsdown.util.SysInfo;
import org.jadestudio.dsdown.util.SysTools;
import org.springframework.stereotype.Component;

@Component(value="/jadestudio/gameManage")
public class GameManageAction  extends DispatchAction {
	public GameManageAction(){}
	@Resource
	private GameManageService gameManageService;
	@Resource
	private ManageLoginService manageLoginService;
	@Resource
	SysInfo sysinfo;
	
	/**
	 * 地址转向
	 * @param request
	 * @param mapping
	 * @param control 转向地址
	 * @param flag 是否检测  1带检测session / 0不带检测session
	 * @return
	 */
	private ActionForward gotoForward(HttpServletRequest request,ActionMapping mapping,String control,int flag){
		if(flag==1){
			int sessionFlag=manageLoginService.userSessionRecheck(request);
			if(sessionFlag==0){
			    ActionForward forward = new ActionForward("/jadestudio/portal.do");
				forward.setRedirect(true);
				return forward;
			}else{
				request.setAttribute("control",control);
				return mapping.findForward("gotoGameManage");
			}
		}else{
			request.setAttribute("control",control);
			return mapping.findForward("gotoGameManage");
		}
	}
	
	
	/**
	 * 引导到厂商管理
	 */
	@SuppressWarnings("unchecked")
	public ActionForward gotoPublishCompanyList(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		
		Map queryMap = SysTools.getParameterMap(request);
		queryMap.put("manageRecordsPerPage", SysInfo.manageRecordsPerPage);
		/*装载当前页数*/
		if(queryMap.get("CurrentPage")==null){
			queryMap.put("CurrentPage", "1");
			queryMap.put("CurrentPageInSql", 0);
		}else if(queryMap.get("CurrentPage").toString().equals("")){
			queryMap.put("CurrentPage", "1");
			queryMap.put("CurrentPageInSql", 0);
		}else{
			queryMap.put("CurrentPageInSql", (Integer.valueOf(queryMap.get("CurrentPage").toString())-1)*SysInfo.manageRecordsPerPage);
		}
		/*结果集*/
		List<Map> list = gameManageService.publishCompanyQueryByPaging(queryMap);
		request.setAttribute("list", list);
		/*装载总条数及总页数*/
		int maxRecords=gameManageService.publishCompanyCountQuery(queryMap);
		int maxPages=maxRecords%SysInfo.manageRecordsPerPage!=0?maxRecords/SysInfo.manageRecordsPerPage+1:maxRecords/SysInfo.manageRecordsPerPage;
		queryMap.put("MaxRecords", maxRecords);
		queryMap.put("MaxPages", maxPages);
		/*装载条件*/
		request.setAttribute("queryMap", queryMap);
		return gotoForward(request,mapping,"gotoPublishCompanyList",1);
	}
	/**
	 * 引导到厂商新增
	 */  
	public ActionForward gotoPublishCompanyAdd(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		request.setAttribute("flag", "add");
		return gotoForward(request,mapping,"gotoPublishCompanyAddorModify",1);
	}
	
	/**
	 * （图鉴录入中的）引导到厂商新增
	 */
	public ActionForward gotoPublishCompanyAdd2(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
			request.setAttribute("flag", "add");
			request.setAttribute("addedExit", "true");//录入后关闭自身
			int sessionFlag=manageLoginService.userSessionRecheck(request);
			if(sessionFlag==0){
			    ActionForward forward = new ActionForward("/jadestudio/portal.do");
				forward.setRedirect(true);
				return forward;
			}else{
				return mapping.findForward("gotoCompanyAddorModify");
			}
	}
	/**
	 * 厂商新增逻辑
	 */
	public ActionForward publishCompanyAdd(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		request.setCharacterEncoding("utf-8");
		int sessionFlag=manageLoginService.userSessionRecheck(request);
		if(sessionFlag==0){
		    ActionForward forward = new ActionForward("/jadestudio/portal.do");
			forward.setRedirect(true);
			return forward;
		}else{
			
			String gamePublishCompany = request.getParameter("game_publish_company")!=null?request.getParameter("game_publish_company"):"";//厂商名称
			String gamePublishZone = request.getParameter("game_publish_zone")!=null?request.getParameter("game_publish_zone"):"";//地区
			String gamePublishUrl = request.getParameter("game_publish_url")!=null?request.getParameter("game_publish_url"):"";//官方网站
			ManageGamePublishCompany manageGamePublishCompany = new ManageGamePublishCompany();
			manageGamePublishCompany.setGamePublishCompany(gamePublishCompany);
			manageGamePublishCompany.setGamePublishUrl(gamePublishUrl);
			manageGamePublishCompany.setGamePublishZone(gamePublishZone);
			manageGamePublishCompany.setGamePublishCompanyId(SysTools.getUuid());
			gameManageService.publishCompanyAdd(manageGamePublishCompany);
			String addedExit = request.getParameter("addedExit")!=null?request.getParameter("addedExit"):"";//是否在其他页中引用
			if(addedExit.equals("true")){
				PrintWriter pw = response.getWriter();
				pw.append("<script>self.close()</script>");//关闭自身
				return null;
			}else{
			ActionForward forward = new ActionForward("/jadestudio/gameManage.do?pm=gotoPublishCompanyList");
			forward.setRedirect(true);
			return forward;
			}
		}
	}
	
	/**
	 * 引导到厂商修改
	 */
	public ActionForward gotoPublishCompanyModify(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		int sessionFlag=manageLoginService.userSessionRecheck(request);
		if(sessionFlag==0){
		    ActionForward forward = new ActionForward("/jadestudio/portal.do");
			forward.setRedirect(true);
			return forward;
			
		}else{
			Map manageGamePublishCompanyMap=gameManageService.publishCompanyQueryById(id);
			if(manageGamePublishCompanyMap!=null){
				request.setAttribute("manageGamePublishCompanyMap", manageGamePublishCompanyMap);
			}
		}
		request.setAttribute("flag", "modify");
		return gotoForward(request,mapping,"gotoPublishCompanyAddorModify",1);
	}
	
	/**
	 * 厂商修改逻辑
	 */
	public ActionForward publishCompanyModify(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		int sessionFlag=manageLoginService.userSessionRecheck(request);
		if(sessionFlag==0){
		    ActionForward forward = new ActionForward("/jadestudio/portal.do");
			forward.setRedirect(true);
			return forward;
		}else{
			String gamePublishCompanyId = request.getParameter("game_publish_company_id")!=null?request.getParameter("game_publish_company_id"):"";//厂商ID
			String gamePublishCompany = request.getParameter("game_publish_company")!=null?request.getParameter("game_publish_company"):"";//厂商名称
			String gamePublishZone = request.getParameter("game_publish_zone")!=null?request.getParameter("game_publish_zone"):"";//地区
			String gamePublishUrl = request.getParameter("game_publish_url")!=null?request.getParameter("game_publish_url"):"";//官方网站
			ManageGamePublishCompany manageGamePublishCompany = new ManageGamePublishCompany();
			manageGamePublishCompany.setGamePublishCompany(gamePublishCompany);
			manageGamePublishCompany.setGamePublishUrl(gamePublishUrl);
			manageGamePublishCompany.setGamePublishZone(gamePublishZone);
			manageGamePublishCompany.setGamePublishCompanyId(gamePublishCompanyId);
			gameManageService.publishCompanyModify(manageGamePublishCompany);
			ActionForward forward = new ActionForward("/jadestudio/gameManage.do?pm=gotoPublishCompanyList");
			forward.setRedirect(true);
			return forward;
		}
	}
	/**
	 * 厂商删除
	 */
	public ActionForward publishCompanyDelete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		int sessionFlag=manageLoginService.userSessionRecheck(request);
		if(sessionFlag==0){
		    ActionForward forward = new ActionForward("/jadestudio/portal.do");
			forward.setRedirect(true);
			return forward;
		}else{
			int indexOfFlag = id.indexOf(",");
			if(indexOfFlag==-1){
					gameManageService.publishCompanyDeleteById(id);
			}else{
				String[] ids=id.split(",");
				for(int t=0;t<ids.length;t++){
					gameManageService.publishCompanyDeleteById(ids[t]);
				}
			}
			ActionForward forward = new ActionForward("/jadestudio/gameManage.do?pm=gotoPublishCompanyList");
			forward.setRedirect(true);
			return forward;
		}
	}
	/**
	 * 引导到游戏（图鉴）管理
	 */
	public ActionForward gotoGameList(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		Map queryMap = SysTools.getParameterMap(request);
		queryMap.put("manageRecordsPerPage", SysInfo.manageRecordsPerPage);
		/*装载当前页数*/
		if(queryMap.get("CurrentPage")==null){
			queryMap.put("CurrentPage", "1");
			queryMap.put("CurrentPageInSql", 0);
		}else if(queryMap.get("CurrentPage").toString().equals("")){
			queryMap.put("CurrentPage", "1");
			queryMap.put("CurrentPageInSql", 0);
		}else{
			queryMap.put("CurrentPageInSql", (Integer.valueOf(queryMap.get("CurrentPage").toString())-1)*SysInfo.manageRecordsPerPage);
		}
		/*结果集*/
		List<Map> list = this.gameManageService.gameQuery(queryMap);
		request.setAttribute("list", list);
		/*装载总条数及总页数*/
		int maxRecords=gameManageService.gameCountQuery(queryMap);
		int maxPages=maxRecords%SysInfo.manageRecordsPerPage!=0?maxRecords/SysInfo.manageRecordsPerPage+1:maxRecords/SysInfo.manageRecordsPerPage;
		queryMap.put("MaxRecords", maxRecords);
		queryMap.put("MaxPages", maxPages);
		/*装载条件*/
		request.setAttribute("queryMap", queryMap);
		request.setAttribute("gameType", this.gameManageService.gameTypeQuery());
		request.setAttribute("gamePlatForm", this.gameManageService.gamePlatFormQuery());
		return gotoForward(request,mapping,"gotolist",1);
	}
	/**
	 * 引导到游戏新增
	 */
	public ActionForward gotoGameAdd(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		request.setAttribute("flag", "add");
		request.setAttribute("gameType", this.gameManageService.gameTypeQuery());
		request.setAttribute("gamePlatform", this.gameManageService.gamePlatFormQuery());
		return gotoForward(request,mapping,"gotoGameAddorModify",1);
	}
	
	/**
	 * 引导到游戏修改
	 */
	public ActionForward gotoGameModify(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		int sessionFlag=manageLoginService.userSessionRecheck(request);
		if(sessionFlag==0){
		    ActionForward forward = new ActionForward("/jadestudio/portal.do");
			forward.setRedirect(true);
			return forward;
		}else{
			/*加载游戏数据*/
			Map gameMap =this.gameManageService.gameQueryById(id);
			if(gameMap!=null){
				request.setAttribute("gameMap", gameMap);
			}
			/*加载游戏下载地址*/
			List<java.util.Map> gameDownloadsMap = this.gameManageService.gameDownloadsQueryByGameId(id);
			if(gameDownloadsMap!=null){
				request.setAttribute("gameDownloadsMap", gameDownloadsMap);
			}
			/*加载游戏购买地址*/
			List<java.util.Map> gamePurchaseMap = this.gameManageService.gamePurchaseQueryByGameId(id);
			if(gamePurchaseMap!=null){
				request.setAttribute("gamePurchaseMap", gamePurchaseMap);
			}
			/*加载游戏图片上传地址*/
			List<java.util.Map> gamePicMap = this.gameManageService.gamePicQueryByGameId(id);
			if(gamePicMap!=null){
				request.setAttribute("gamePicMap", gamePicMap);
			}
			
			
		}
		request.setAttribute("flag", "modify");
		request.setAttribute("gameType", this.gameManageService.gameTypeQuery());
		request.setAttribute("gamePlatform", this.gameManageService.gamePlatFormQuery());
		return gotoForward(request,mapping,"gotoGameAddorModify",1);
	}
	
	/**
	 * 游戏厂商查询（返回json）
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	public void getGamePublishCompanyByName(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		int sessionFlag=manageLoginService.userSessionRecheck(request);
		response.setContentType("text/html");   
        response.setCharacterEncoding("UTF-8"); 
		PrintWriter out = response.getWriter();   
		if(sessionFlag==0){
				out.write("[{\"id\":\"\",\"name\":\"SESSION丢失\"}]");   
		        out.flush();   
		        out.close(); 
		}else{
	        String requestString = request.getParameter("q");
	        requestString = new String(requestString.getBytes("iso8859-1"),"UTF-8");
	        List<Map> lt =this.gameManageService.gamePublishCompanyQuery(requestString);
	        if(lt.size()>0){
		        //页面显示值   
		        String[] name=new String[lt.size()];
		        //实际值   
		        String[] id=new String[lt.size()];
		        for(int t=0;t<lt.size();t++){
		        	name[t]=lt.get(t).get("game_publish_company")!=null?lt.get(t).get("game_publish_company").toString():"";
		        	id[t]=lt.get(t).get("game_publish_company_id")!=null?lt.get(t).get("game_publish_company_id").toString():"";
		        }
		        StringBuffer sb = new StringBuffer("[");   
		        for(int i = 0;i < id.length;i++){   
		            sb.append("{\"id\":\"" + id[i] + "\",\"name\":\"" + name[i] +  "\"},");   
		        }   
		        sb.deleteCharAt(sb.lastIndexOf(",")).append("]");   
		        out.write(sb.toString());   
		        out.flush();   
		        out.close(); 
	        }else{
	        	out.write("[{\"id\":\"\",\"name\":\"不存在的数据\"}]");   
		        out.flush();   
		        out.close(); 
	        }
		}
	}
	
	/**
	 * 游戏新增
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public ActionForward gameAdd(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		int sessionFlag=manageLoginService.userSessionRecheck(request);
		if(sessionFlag==0){
		    ActionForward forward = new ActionForward("/jadestudio/portal.do");
			forward.setRedirect(true);
			return forward;
		}else{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String  simplifiedChineseName= request.getParameter("simplified_chinese_name")!=null?request.getParameter("simplified_chinese_name"):"";//游戏名称
			String  traditionalChineseName= request.getParameter("traditional_chinese_name")!=null?request.getParameter("traditional_chinese_name"):"";//繁体名称
			String  englishName = request.getParameter("english_name")!=null?request.getParameter("english_name"):"";//英文名称
			String  japaneseName = request.getParameter("japanese_name")!=null?request.getParameter("japanese_name"):"";//日文名称
			String  gameTag= request.getParameter("game_tag")!=null?request.getParameter("game_tag"):"";//游戏TAG
			String[]  gamePlatFormIdArr=(String[])request.getParameterValues("game_platform_id");//游戏平台
			String gamePlatFormId="";
			if(gamePlatFormIdArr!=null){ //&& gamePlatFormIdArr.length>=10
				 for(String tmp:gamePlatFormIdArr){
					 if(!tmp.equals("")){
						 gamePlatFormId+=(tmp+",");
					 }
				 } 
				 if(!gamePlatFormId.equals(""))
					 gamePlatFormId=gamePlatFormId.substring(0,gamePlatFormId.lastIndexOf(","));
			}
			String  gameTypeId= request.getParameter("game_type_id")!=null?request.getParameter("game_type_id"):"";//游戏类型
			String  gameImgNormal= request.getParameter("game_img_normal")!=null?request.getParameter("game_img_normal"):"";//游戏大图
			String  gameImgSmall= request.getParameter("game_img_small")!=null?request.getParameter("game_img_small"):"";//游戏小图
			String  devCompany= request.getParameter("dev_company")!=null?request.getParameter("dev_company"):"";//开发商
			String  publishCompanyId = request.getParameter("publish_company_id")!=null?request.getParameter("publish_company_id"):"";//发行商
			String  gameLanguage="";
			String  gameLanguageJpn= request.getParameter("game_language_jpn")!=null?request.getParameter("game_language_jpn"):"";//游戏语言(日)
			String  gameLanguageEng= request.getParameter("game_language_eng")!=null?request.getParameter("game_language_eng"):"";//游戏语言(英)
			String  gameLanguageScn= request.getParameter("game_language_scn")!=null?request.getParameter("game_language_scn"):"";//游戏语言(简体中文)
			String  gameLanguageTcn= request.getParameter("game_language_tcn")!=null?request.getParameter("game_language_tcn"):"";//游戏语言(繁体中文)
			String  gameLanguageKon= request.getParameter("game_language_kon")!=null?request.getParameter("game_language_kon"):"";//韩国语
			String  gameLanguageEtc= request.getParameter("game_language_etc")!=null?request.getParameter("game_language_etc"):"";//其他
			
			if(gameLanguageJpn.equals("on")){
				gameLanguage+="jpn,";
			}
			if(gameLanguageEng.equals("on")){
				gameLanguage+="eng,";
			}
			if(gameLanguageScn.equals("on")){
				gameLanguage+="chs,";
			}
			if(gameLanguageTcn.equals("on")){
				gameLanguage+="cht,";
			}
			if(gameLanguageKon.equals("on")){
				gameLanguage+="kon,";
			}
			if(gameLanguageEtc.equals("on")){
				gameLanguage+="oth,";
			}
			
			if(gameLanguage.indexOf(",")!=-1){
				gameLanguage=gameLanguage.substring(0,gameLanguage.lastIndexOf(","));
			}
			
			
			String  publishDate= request.getParameter("publish_date")!=null?request.getParameter("publish_date"):"";//发行日期
			String[]  famiScoreArr=(String[])request.getParameterValues("fami_score");//Fami通评分
			String[]  ignScoreArr= (String[])request.getParameterValues("ign_score");//IGN评分
			String famiScore="";
			String ignScore="";
			if(famiScoreArr!=null){ //&& famiScoreArr.length>=4
				 for(String tmp:famiScoreArr){
					 if(!tmp.equals("")){
						 famiScore+=(tmp+",");
					 }
				 } 
				 if(!famiScore.equals(""))
					 famiScore=famiScore.substring(0,famiScore.lastIndexOf(","));
			}
			
			if(ignScoreArr!=null){ //&& ignScoreArr.length>=5
				 for(String tmp:ignScoreArr){
					 if(!tmp.equals("")){
						 ignScore+=(tmp+",");
					 }
				 } 
				 if(!ignScore.equals(""))
				 ignScore=ignScore.substring(0,ignScore.lastIndexOf(","));
			}

			String  gamePrice= request.getParameter("game_price")!=null?request.getParameter("game_price"):"";//游戏售价 
			String  gameIntroduction= request.getParameter("game_introduction_value")!=null?request.getParameter("game_introduction_value"):"";//游戏介绍
			String  gameDownloadsEnable= request.getParameter("game_downloads_enable")!=null?request.getParameter("game_downloads_enable"):"";//是否开启下载
			/***********************************************/
			String[] gameDownloadsName=request.getParameterValues("game_downloads_name");//游戏下载名称
			String[] gameDownloadsUrl=request.getParameterValues("game_downloads_url");//游戏下载地址
			/***********************************************/
			String  gamePurchaseEnable= request.getParameter("game_purchase_enable")!=null?request.getParameter("game_purchase_enable"):"";//是否开启购买
			/***********************************************/
			
			String[] gamePurchaseStoreName=request.getParameterValues("game_purchase_store_name");//店铺名
			String[] gamePurchaseUrl=request.getParameterValues("game_purchase_url");//购买地址
			String[] gamePurchasePrice=request.getParameterValues("game_purchase_price");//价格
			String[] gamePurchaseTitle=request.getParameterValues("game_purchase_title");//商品名
			String[] gamePurchaseStoreUrl=request.getParameterValues("game_purchase_store_url");//店铺地址
			String[] gamePurchaseFlag=request.getParameterValues("game_purchase_flag");//是否VIP添加标记
			String[] gamePurchasePicUrl=request.getParameterValues("game_purchase_pic_url");//图片地址
			
			/***********************************************/
			String[] gamePicUrl = request.getParameterValues("game_pic_url");//游戏图片地址
			/***********************************************/
			String  gameSearchIndex= request.getParameter("game_search_index")!=null?request.getParameter("game_search_index"):"";//游戏索引字母
			String  gameEnable= request.getParameter("game_enable")!=null?request.getParameter("game_enable"):"";//游戏开启
			String  gameIsRedirect = request.getParameter("game_is_redirect")!=null?request.getParameter("game_is_redirect"):"";//是否跳转
			String  gameRedirectUrl= request.getParameter("game_redirect_url")!=null?request.getParameter("game_redirect_url"):"";//跳转地址
			String  gamePositionTop= request.getParameter("game_position_top")!=null?request.getParameter("game_position_top"):"";//是否置顶
			ManageGame mg = new ManageGame();
			String gameId = SysTools.getUuid();
			mg.setGameId(gameId);
			mg.setSimplifiedChineseName(simplifiedChineseName);
			mg.setTraditionalChineseName(traditionalChineseName);
			mg.setEnglishName(englishName);
			mg.setJapaneseName(japaneseName);
			mg.setGameTag(gameTag);
			mg.setGameImgNormal(gameImgNormal);
			mg.setGameImgSmall(gameImgSmall);
			mg.setDevCompany(devCompany);
			mg.setPublishCompanyId(publishCompanyId);
			mg.setGameLanguage(gameLanguage);//new add
			mg.setGameTypeId(gameTypeId);
			mg.setGamePlatFormId(gamePlatFormId);//new add
			mg.setPublishDate(publishDate);
			mg.setFamiScore(famiScore);//new add
			mg.setIgnScore(ignScore);//new add
			mg.setGamePrice(gamePrice);
			mg.setGameIntroduction(gameIntroduction);
			mg.setGameCreateDate(sdf.format(new Date()));
			mg.setGameClick(0);
			mg.setGameSearchIndex(gameSearchIndex);
			mg.setGameEnable(gameEnable);
			mg.setGamePositionTop(gamePositionTop);
			mg.setGameDownloadsEnable(gameDownloadsEnable);
			mg.setGameIsRedirect(gameIsRedirect);
			mg.setGamePurchaseEnable(gamePurchaseEnable);
			mg.setGameRedirectUrl(gameRedirectUrl);
			int flag=this.gameManageService.gameInsert(mg);
			if(flag==1){
				/*下载地址*/
				if(gameDownloadsUrl!=null){
					ManageGameDownloads mgd=null;
					for(int t=0;t<gameDownloadsUrl.length;t++){
						mgd = new ManageGameDownloads();
						mgd.setGame_downloads_id(SysTools.getUuid());
						mgd.setGame_downloads_url(gameDownloadsUrl[t]);
						mgd.setGame_downloads_name(gameDownloadsName[t]);
						mgd.setGame_id(gameId);
					    this.gameManageService.gameDownloadsInsert(mgd);           
					}
				}
				
				/*购买地址*/
				if(gamePurchaseUrl!=null){
					ManageGamePurchase mgp=null;
					for(int t=0;t<gamePurchaseUrl.length;t++){
						mgp= new ManageGamePurchase();
						mgp.setGame_id(gameId);
						mgp.setGame_purchase_id(SysTools.getUuid());
						mgp.setGame_purchase_price(gamePurchasePrice[t]);
						mgp.setGame_purchase_store_name(gamePurchaseStoreName[t]);
						mgp.setGame_purchase_url(gamePurchaseUrl[t]);
						mgp.setGame_purchase_title(SysTools.noHtmlText(gamePurchaseTitle[t]));
						mgp.setGame_purchase_store_url(gamePurchaseStoreUrl[t]);
						mgp.setGame_purchase_flag(gamePurchaseFlag[t]);
						mgp.setGame_purchase_pic_url(gamePurchasePicUrl[t]);
						this.gameManageService.gamePurchaseInsert(mgp);
					}
				}
				/*图片地址*/
				if(gamePicUrl!=null){
					ManageGamePic mgpic=null;
					for(int t=0;t<gamePicUrl.length;t++){
						mgpic= new ManageGamePic();
						mgpic.setGame_id(gameId);
						mgpic.setGame_pic_id(SysTools.getUuid());
						mgpic.setGame_pic_url(gamePicUrl[t]);
						this.gameManageService.gamePicInsert(mgpic);
					}
				}
			}
			ActionForward forward = new ActionForward("/jadestudio/gameManage.do?pm=gotoGameList");
			forward.setRedirect(true);
			return forward;
		}
	}
	
	
	/**
	 * 游戏修改
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public ActionForward gameModify(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		int sessionFlag=manageLoginService.userSessionRecheck(request);
		if(sessionFlag==0){
		    ActionForward forward = new ActionForward("/jadestudio/portal.do");
			forward.setRedirect(true);
			return forward;
		}else{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String  gameId= request.getParameter("game_id")!=null?request.getParameter("game_id"):"";//游戏名称
			String  simplifiedChineseName= request.getParameter("simplified_chinese_name")!=null?request.getParameter("simplified_chinese_name"):"";//游戏名称
			String  traditionalChineseName= request.getParameter("traditional_chinese_name")!=null?request.getParameter("traditional_chinese_name"):"";//繁体名称
			String  englishName = request.getParameter("english_name")!=null?request.getParameter("english_name"):"";//英文名称
			String  japaneseName = request.getParameter("japanese_name")!=null?request.getParameter("japanese_name"):"";//日文名称
			String  gameTag= request.getParameter("game_tag")!=null?request.getParameter("game_tag"):"";//游戏TAG
			String  gameTypeId= request.getParameter("game_type_id")!=null?request.getParameter("game_type_id"):"";//游戏类型
			String  gameImgNormal= request.getParameter("game_img_normal")!=null?request.getParameter("game_img_normal"):"";//游戏大图
			String  gameImgSmall= request.getParameter("game_img_small")!=null?request.getParameter("game_img_small"):"";//游戏小图
			String  devCompany= request.getParameter("dev_company")!=null?request.getParameter("dev_company"):"";//开发商
			String  publishCompanyId = request.getParameter("publish_company_id")!=null?request.getParameter("publish_company_id"):"";//发行商
			String  publishDate= request.getParameter("publish_date")!=null?request.getParameter("publish_date"):"";//发行日期
			
			String  gameLanguage="";
			String  gameLanguageJpn= request.getParameter("game_language_jpn")!=null?request.getParameter("game_language_jpn"):"";//游戏语言(日)
			String  gameLanguageEng= request.getParameter("game_language_eng")!=null?request.getParameter("game_language_eng"):"";//游戏语言(英)
			String  gameLanguageScn= request.getParameter("game_language_scn")!=null?request.getParameter("game_language_scn"):"";//游戏语言(简体中文)
			String  gameLanguageTcn= request.getParameter("game_language_tcn")!=null?request.getParameter("game_language_tcn"):"";//游戏语言(繁体中文)
			String  gameLanguageEtc= request.getParameter("game_language_etc")!=null?request.getParameter("game_language_etc"):"";//其他
			String  gameLanguageKon= request.getParameter("game_language_kon")!=null?request.getParameter("game_language_kon"):"";//韩国语
			
			if(gameLanguageJpn.equals("on")){
				gameLanguage+="jpn,";
			}
			if(gameLanguageEng.equals("on")){
				gameLanguage+="eng,";
			}
			if(gameLanguageScn.equals("on")){
				gameLanguage+="chs,";
			}
			if(gameLanguageTcn.equals("on")){
				gameLanguage+="cht,";
			}
			if(gameLanguageKon.equals("on")){
				gameLanguage+="kon,";
			}
			if(gameLanguageEtc.equals("on")){
				gameLanguage+="oth,";
			}
			
			if(gameLanguage.indexOf(",")!=-1){
				gameLanguage=gameLanguage.substring(0,gameLanguage.lastIndexOf(","));
			}

			String[]  famiScoreArr=(String[])request.getParameterValues("fami_score");//Fami通评分
			String[]  ignScoreArr= (String[])request.getParameterValues("ign_score");//IGN评分
			String famiScore="";
			String ignScore="";
			if(famiScoreArr!=null){ //&& famiScoreArr.length>=4
				 for(String tmp:famiScoreArr){
					 if(!tmp.equals("")){
						 famiScore+=(tmp+",");
					 }
				 } 
				 if(!famiScore.equals(""))
				 famiScore=famiScore.substring(0,famiScore.lastIndexOf(","));
			}
			if(ignScoreArr!=null){ //&& ignScoreArr.length>=5
				 for(String tmp:ignScoreArr){
					 if(!tmp.equals("")){
						 ignScore+=(tmp+",");
					 }
				 } 
				 if(!ignScore.equals(""))
				 ignScore=ignScore.substring(0,ignScore.lastIndexOf(","));
			}
			
			String[]  gamePlatFormIdArr=(String[])request.getParameterValues("game_platform_id");//游戏平台
			String gamePlatFormId="";
			if(gamePlatFormIdArr!=null){ //&& gamePlatFormIdArr.length>=10
				 for(String tmp:gamePlatFormIdArr){
					 if(!tmp.equals("")){
						 gamePlatFormId+=(tmp+",");
					 }
				 }
				 if(!gamePlatFormId.equals(""))
					 gamePlatFormId=gamePlatFormId.substring(0,gamePlatFormId.lastIndexOf(","));
			}
			
			
			
			String  gamePrice= request.getParameter("game_price")!=null?request.getParameter("game_price"):"";//游戏售价 
			String  gameIntroduction= request.getParameter("game_introduction_value")!=null?request.getParameter("game_introduction_value"):"";//游戏介绍
			String  gameDownloadsEnable= request.getParameter("game_downloads_enable")!=null?request.getParameter("game_downloads_enable"):"";//是否开启下载
			/***********************************************/
			String[] gameDownloadsId=request.getParameterValues("game_downloads_id");//游戏下载ID
			String[] gameDownloadsName=request.getParameterValues("game_downloads_name");//游戏下载名称
			String[] gameDownloadsUrl=request.getParameterValues("game_downloads_url");//游戏下载地址
			/***********************************************/
			String  gamePurchaseEnable= request.getParameter("game_purchase_enable")!=null?request.getParameter("game_purchase_enable"):"";//是否开启购买
			/***********************************************/
			String[] gamePurchaseId=request.getParameterValues("game_purchase_id");//游戏购买ID
			String[] gamePurchaseStoreName=request.getParameterValues("game_purchase_store_name");//店铺名
			String[] gamePurchaseUrl=request.getParameterValues("game_purchase_url");//购买地址
			String[] gamePurchasePrice=request.getParameterValues("game_purchase_price");//价格
			String[] gamePurchaseTitle=request.getParameterValues("game_purchase_title");//商品名
			String[] gamePurchaseStoreUrl=request.getParameterValues("game_purchase_store_url");//店铺地址
			String[] gamePurchaseFlag=request.getParameterValues("game_purchase_flag");//是否VIP添加标记
			String[] gamePurchasePicUrl=request.getParameterValues("game_purchase_pic_url");//图片地址
			/***********************************************/
			String[] gamePicId = request.getParameterValues("game_pic_id");//游戏图片Id
			String[] gamePicUrl = request.getParameterValues("game_pic_url");//游戏图片地址
			/***********************************************/
			String  gameSearchIndex= request.getParameter("game_search_index")!=null?request.getParameter("game_search_index"):"";//游戏索引字母
			String  gameEnable= request.getParameter("game_enable")!=null?request.getParameter("game_enable"):"";//游戏开启
			String  gameIsRedirect = request.getParameter("game_is_redirect")!=null?request.getParameter("game_is_redirect"):"";//是否跳转
			String  gameRedirectUrl= request.getParameter("game_redirect_url")!=null?request.getParameter("game_redirect_url"):"";//跳转地址
			String  gamePositionTop= request.getParameter("game_position_top")!=null?request.getParameter("game_position_top"):"";//是否置顶
			ManageGame mg = new ManageGame();
			mg.setGameId(gameId);
			mg.setSimplifiedChineseName(simplifiedChineseName);
			mg.setTraditionalChineseName(traditionalChineseName);
			mg.setEnglishName(englishName);
			mg.setJapaneseName(japaneseName);
			mg.setGameTag(gameTag);
			mg.setGameImgNormal(gameImgNormal);
			mg.setGameImgSmall(gameImgSmall);
			mg.setDevCompany(devCompany);
			mg.setPublishCompanyId(publishCompanyId);
			mg.setGamePlatFormId(gamePlatFormId);//new add
			mg.setGameLanguage(gameLanguage);//new add
			mg.setGameTypeId(gameTypeId);
			mg.setPublishDate(publishDate);
			mg.setFamiScore(famiScore);//new add
			mg.setIgnScore(ignScore);//new add
			mg.setGamePrice(gamePrice);
			mg.setGameIntroduction(gameIntroduction);
			mg.setGameCreateDate(sdf.format(new Date()));
			mg.setGameClick(0);
			mg.setGameSearchIndex(gameSearchIndex);
			mg.setGameEnable(gameEnable);
			mg.setGamePositionTop(gamePositionTop);
			mg.setGameDownloadsEnable(gameDownloadsEnable);
			mg.setGameIsRedirect(gameIsRedirect);
			mg.setGamePurchaseEnable(gamePurchaseEnable);
			mg.setGameRedirectUrl(gameRedirectUrl);
			int flag=this.gameManageService.gameUpdate(mg);
			if(flag==1){
					/*下载地址*/
					ManageGameDownloads mgd=null;
					this.gameManageService.gameDownloadsDelete(gameId);
					if(gameDownloadsId!=null){
						for(int t=0;t<gameDownloadsId.length;t++){
								mgd = new ManageGameDownloads();
								mgd.setGame_downloads_id(SysTools.getUuid());
								mgd.setGame_downloads_url(gameDownloadsUrl[t]);
								mgd.setGame_downloads_name(gameDownloadsName[t]);
								mgd.setGame_id(gameId);
								this.gameManageService.gameDownloadsInsert(mgd);
						}
					}
				
					/*购买地址*/
					this.gameManageService.gamePurchaseDelete(gameId);
					ManageGamePurchase mgp=null;
					if(gamePurchaseId!=null){
						for(int t=0;t<gamePurchaseId.length;t++){
								mgp= new ManageGamePurchase();
								mgp.setGame_id(gameId);
								mgp.setGame_purchase_id(SysTools.getUuid());
								mgp.setGame_purchase_price(gamePurchasePrice[t]);
								mgp.setGame_purchase_store_name(gamePurchaseStoreName[t]);
								mgp.setGame_purchase_url(gamePurchaseUrl[t]);
								mgp.setGame_purchase_title(SysTools.noHtmlText(gamePurchaseTitle[t]));
								mgp.setGame_purchase_store_url(gamePurchaseStoreUrl[t]);
								mgp.setGame_purchase_flag(gamePurchaseFlag[t]);
								mgp.setGame_purchase_pic_url(gamePurchasePicUrl[t]);
								this.gameManageService.gamePurchaseInsert(mgp);
						}
					}
					/*图片地址*/
					this.gameManageService.gamePicDelete(gameId);
					ManageGamePic mgpic=null;
					if(gamePicId!=null){
						for(int t=0;t<gamePicId.length;t++){
							//if(gamePicId[t]!=null && !gamePicId[t].equals("")){
							//	mgpic= new ManageGamePic();
							//	mgpic.setGame_id(gameId);
							//	mgpic.setGame_pic_id(gamePicId[t]);
							//	mgpic.setGame_pic_url(gamePicUrl[t]);
							//	this.gameManageService.gamePicUpdate(mgpic);         
							//}else{
								mgpic= new ManageGamePic();
								mgpic.setGame_id(gameId);
								mgpic.setGame_pic_id(SysTools.getUuid());
								mgpic.setGame_pic_url(gamePicUrl[t]);
								this.gameManageService.gamePicInsert(mgpic);
							//}
						}
					}
			}
			ActionForward forward = new ActionForward("/jadestudio/gameManage.do?pm=gotoGameList");
			forward.setRedirect(true);
			return forward;
		}
	}
	
	/**
	 * 游戏删除
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward gameDelete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		int sessionFlag=manageLoginService.userSessionRecheck(request);
		if(sessionFlag==0){
		    ActionForward forward = new ActionForward("/jadestudio/portal.do");
			forward.setRedirect(true);
			return forward;
		}else{
			String id= request.getParameter("id");
			int indexOfFlag = id.indexOf(",");
			if(indexOfFlag==-1){
					gameManageService.gameDeleteById(id);
			}else{
				String[] ids=id.split(",");
				for(int t=0;t<ids.length;t++){
					gameManageService.gameDeleteById(ids[t]);
				}
			}
			ActionForward forward = new ActionForward("/jadestudio/gameManage.do?pm=gotoGameList");
			forward.setRedirect(true);
			return forward;
		}
	}
	
	
	/**
	 * 进入游戏主页大图配置
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward gotoHomePagePicCfg(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		int sessionFlag=manageLoginService.userSessionRecheck(request);
		if(sessionFlag==0){
		    ActionForward forward = new ActionForward("/jadestudio/portal.do");
			forward.setRedirect(true);
			return forward;
		}else{
			request.setAttribute("homePagePicCfgList", this.gameManageService.homePagePicCfgQuery());
			return gotoForward(request,mapping,"gotoHomePagePicCfg",1);
		}
	}
	
	/**
	 * 大图配置修改
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward insertPagePicCfg(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		int sessionFlag=manageLoginService.userSessionRecheck(request);
		if(sessionFlag==0){
		    ActionForward forward = new ActionForward("/jadestudio/portal.do");
			forward.setRedirect(true);
			return forward;
		}else{
			this.gameManageService.homePagePicCfgDelete();
			String[] feedTitleArr=request.getParameterValues("feed_title");
			String[] feedImageArr=request.getParameterValues("feed_image");
			String[] feedUrlArr=request.getParameterValues("feed_url");
			String[] feedEnableArr=request.getParameterValues("feed_enable");
			for(int t=0;t<feedTitleArr.length;t++){
				ManageHomePagePicCfg mp = new ManageHomePagePicCfg();
				mp.setFeed_title(feedTitleArr[t]!=null?feedTitleArr[t].toString():"");
				mp.setFeed_image(feedImageArr[t]!=null?feedImageArr[t].toString():"");
				mp.setFeed_url(feedUrlArr[t]!=null?feedUrlArr[t].toString():"");
				mp.setFeed_enable(feedEnableArr[t]!=null?feedEnableArr[t].toString():"");
				this.gameManageService.homePagePicCfgInsert(mp);
			}
			request.setAttribute("homePagePicCfgList", this.gameManageService.homePagePicCfgQuery());
			return gotoForward(request,mapping,"gotoHomePagePicCfg",1);
		}
	}
	
	/**
	 * 进入服务器修改页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward gotoServerCfg(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		int sessionFlag=manageLoginService.userSessionRecheck(request);
		if(sessionFlag==0){
		    ActionForward forward = new ActionForward("/jadestudio/portal.do");
			forward.setRedirect(true);
			return forward;
		}else{
			request.setAttribute("serverCfgList", this.gameManageService.serverCfgQuery());
			return gotoForward(request,mapping,"gotoServerCfg",1);
		}
	}
	
	
	/**
	 * 进入服务器更新
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateServerCfg(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		int sessionFlag=manageLoginService.userSessionRecheck(request);
		if(sessionFlag==0){
		    ActionForward forward = new ActionForward("/jadestudio/portal.do");
			forward.setRedirect(true);
			return forward;
		}else{
			String[] keys=request.getParameterValues("key");
			String[] values=request.getParameterValues("value");
			String[] enables=request.getParameterValues("enable");
			Map map;
			for(int t=0;t<keys.length;t++){
				map =new HashMap();
				map.put("key", keys[t]);
				map.put("value", values[t]);
				map.put("enable", enables[t]);
				this.gameManageService.serverCfgUpdate(map);
			}
			
			sysinfo.reload();
			request.setAttribute("serverCfgList", this.gameManageService.serverCfgQuery());
			return gotoForward(request,mapping,"gotoServerCfg",1);
		}
	}
	
	
	
	
	
}
